#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    Part of Seagull - The Pyhon Pages engine
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Python Page Compiler -- Main
"""
__author__ = 'Jonathan Slenders <jonathan at slenders dot be>'
__license__ = 'GNU LGPL'

__all__ = ('PageCompiler', )

#import code_generator
import code_generator
import lexer
import parser
import writeback

from compile_exceptions import CompileException

class Compiler(object):
	""" Compiles a python page """
	def __init__(self, s, path, debug=False):
		self.path = path

		try:
			# Lex the source code
			l = lexer.Lexer(s).get_result()

			# OLD

			# Build a compiled page from this
#			self._page = code_generator.CodeGenerator(l, insert_debug_code = debug)

			# NEW
			p = parser.Parser(l).get_result()
			self._page = code_generator.CodeGenerator(p)

		except CompileException, e:
			raise CompileException(e.line, e.column, e.message, path)

	def get_reflexion_info(self):
		""" Return reflexion XML as string """
		return self._page.get_reflexion_info()

	def get_compiled_script(self):
		""" Return compiled code a string """
		return self._page.get_compiled_code(self.path)




if __name__ == '__main__':
	s = ur'''
<%!page external="false" compress-whitespace="true" disabled="true" %>
<%!param name="test" class="object" type="read-write" %>
<%!filter name="resolve_urls" %>
<%!filter name="morse\ae\\oaoeu\"aoeuu" ~except_div="class_no_morse" ~font="arial" %>
<%!master path="Master" ~param_1="red" ~param2="green" %>
<%
	def init():
		""" initialization function """;
		# Comment
		# comment 2
		print 'this is python';
	enddef;

	if True: print 'this is true'; endif;
		
%> <!-- this is a comment -->
<%!content name="replace this" %>

<[[ <% this is literal]] ]  ]]>

  -->
	<p>
	</p>
	<%:"path/oaeu" p="abc" ~var="value" ~var2="value2" %>
	<%["path/aoeuaoeu" o="abcd" ~v="true" %>
		<%= default_block %>
		<%!content %>
			aoeu
		<%!content name="red" ~title="red..." %>
			aeubb
		<%!content name="nested" %>
		<%["path/nested" %>
			<%!content %>
				aoeuaoe
			<%!content %>
				aoeuaoe
		<%] %>
	<%] %>


	<ul>
		<li>...</li>
		<li>...</li>
		<li>...</li>
		<li>...</li>
	</ul>
<%!content name="Main" %\>
	<p>
		Paragraph 1
	</p>
	<%= 'aoeu\'aeou' %>
	<%:"path/oaeu" ~var="value" ~var2="value2" %>

	<!--  aoeu <% this is all comment  -- aoe 
a	-->
	<% if True: %>
		test
	<% endif; %>
		testje '''

	
	new_content = \
'''
<p>
		new content
	</p>
<%
		python code

		# ...
	%>
	<p> <strong><!-- ..... --></strong></p>
'''


	print s
	print '================='
	p = lexer.Lexer(s).get_result()
	p2 = lexer.Lexer(new_content).get_result()

	import parser, code_generator

	page_parser = parser.Parser(p).get_result()
	#print code_generator.CodeGenerator(page_parser).get_compiled_code('/path')

	print writeback.WriteBack(page_parser).get_result()

#	#	print p
#	import parse_tree_manipulations
#	print parse_tree_manipulations.PageEditor
#	editor = parse_tree_manipulations.PageEditor(p)
#	editor.replace_content('replace this', p2)
#
#	print writeback.WriteBack().to_source(editor.parse_tree)
#	parse_tree_manipulations.check_subtree_safety(p2)
#	#	page = Page(p)
#	#	print page.get_compiled_code('/testpage', 'classxxx')
